package com.viewhigh.yhrp;

import java.util.Random;

import org.apache.commons.codec.binary.Base64;

/**   
* @Title: EnCode.java 
* @Package com.viewhigh.yhrp 
* @Description: 编码
* @author zhangxiaofu   
* @Company www.viewhigh.com
* @date 2016年8月24日 下午2:53:43 
* @version V1.0   
*/
public class EnCodeUtil {

    public static String generate(String param) throws Exception {
        String hex=  Const.base1.concat(param);//转成16进制
        hex = string2HexString(hex);
        
        String hex1= enCross(param);
        hex1 = string2HexString(hex1);
       
        String hex2=  param.concat(Const.base3);
        hex2 = string2HexString(hex2);
         return hex.concat(Const.A).concat(hex1).concat(Const.B).concat(hex2);
     }
    /**
     * 
    * @Title: generateBase64 
    * @Description: Base64显示字符串 
    * @param @param param
    * @param @return
    * @param @throws Exception    设定文件 
    * @return String    返回类型 
    * @throws
     */
    public static String generateBase64(String param) throws Exception{
        String generate = generate(param);
        return new String(Base64.encodeBase64(generate.getBytes()));
    }
    
    /**
     * 
    * @Title: enCross 
    * @Description: 交插 
    * @param @param param
    * @param @return    设定文件 
    * @return String    返回类型 
    * @throws
     */
    public static String enCross(String param){
        String temp= "";
        char[] base = Const.base2.toCharArray();
        for (int i = 0 ; i< param.length() ; i++) {
            String v ;
            if(i < base.length){
                v = base[i]+"";
            }else{
                v = new Random().nextInt(9)+"";
            }
            char[] charArray = param.toCharArray();
            temp +=charArray[i]+v;
        }
        return temp;
    }
    
    /** 
     * @Title:string2HexString 
     * @Description:字符串转16进制字符串 
     * @param strPart 字符串 
     * @return 16进制字符串  
     * @throws 
     */  
    public static String string2HexString(String strPart) {  
        StringBuffer hexString = new StringBuffer();  
        for (int i = 0; i < strPart.length(); i++) {  
            int ch = (int) strPart.charAt(i);  
            String strHex = Integer.toHexString(ch);  
            hexString.append(strHex);  
        }  
        return hexString.toString();  
    }  
    
    public static void main(String[] args) {
        try {
            String k = "adminrt5";
            String key = generate(k);
            String b4 = generateBase64(k);
            System.err.println("key="+key );
            System.err.println("base64 = "+b4);
            //解
            System.out.println("========================================");
            System.err.println(DeCodeUtil.parsing(key));
            System.err.println(DeCodeUtil.parsingBase64(b4));
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

